package com.kjsdfgk.hjmdsf.app.utils;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;


import com.kjsdfgk.hjmdsf.app.App;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.List;

public class SharedPrefsUtils {
	public static final String PREFS_NAME = "live_prefs";
	private static SharedPreferences preferences;

	private static void init() {
		if (preferences == null) {
			preferences = App.getAppContext().getSharedPreferences(PREFS_NAME, 0);
		}
	}

	public static boolean getBoolean(String key, boolean defValue) {
		init();
		return preferences.getBoolean(key, defValue);
	}

	public static int getInt(String key, int defValue) {
		init();
		return preferences.getInt(key, defValue);
	}

	public static String getString(String key, String defValue) {
		init();
		return preferences.getString(key, defValue);
	}

	public static long getLong(String key, long defValue) {
		init();
		return preferences.getLong(key, defValue);
	}

	public static float getFloat(String key, float defValue) {
		init();
		return preferences.getFloat(key, defValue);
	}

	public static void setLong(String key, long value) {
		init();
		Editor editor = preferences.edit();
		editor.putLong(key, value);
		editor.commit();
	}

	public static void setBoolean(String key, boolean value) {
		init();
		Editor editor = preferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static void setString(String key, String value) {
		init();
		Editor editor = preferences.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public static void setInt(String key, int value) {
		init();
		Editor editor = preferences.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public static void setFloat(String key, float value) {
		init();
		Editor editor = preferences.edit();
		editor.putFloat(key, value);
		editor.commit();
	}

	
	public static String SceneList2String(List SceneList)
            throws IOException {
      // 实例化一个ByteArrayOutputStream对象，用来装载压缩后的字节文件。
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      // 然后将得到的字符数据装载到ObjectOutputStream
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(
              byteArrayOutputStream);
      // writeObject 方法负责写入特定类的对象的状态，以便相应的 readObject 方法可以还原它
      objectOutputStream.writeObject(SceneList);
      // 最后，用Base64.encode将字节文件转换成Base64编码保存在String中
      String SceneListString = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
      // 关闭objectOutputStream
      objectOutputStream.close();
      return SceneListString;



}

 

 @SuppressWarnings("unchecked")
  public static List String2SceneList(String SceneListString)
          throws StreamCorruptedException, IOException,
          ClassNotFoundException {
      byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),
              Base64.DEFAULT);
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
              mobileBytes);
      ObjectInputStream objectInputStream = new ObjectInputStream( byteArrayInputStream);
      List SceneList = (List) objectInputStream.readObject();
      objectInputStream.close();
      return SceneList;
  }

}
